package com.rq.base.util;

import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;


import com.rq.base.BASE;
import com.rq.base.R;
import com.rq.base.ui.BaseActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class LanguageUtil {

    BaseActivity activity;

    public LanguageUtil() {
    }

    public LanguageUtil(BaseActivity ac) {
        this.activity = ac;
    }

    public interface LanguageLoadListener {
        //0-中文 1-英文 2-俄文 3-日文 4-韩文
        void success(int lan);
    }

    public void loadLoadSelect(LanguageLoadListener listener) {
        String old = SPUtil.getString("language");
        if (!TextUtils.isEmpty(old)) {
            listener.success(getOption());
        } else {
            listener.success(0);
        }
    }

    public static List<String> getOpt(LANGUAGE... arr) {
        List<String> opts = new ArrayList<>();
        for (LANGUAGE language : arr) {
            opts.add(BASE.getCxt().getResources().getString(language.showRes));
        }
        return opts;
    }

    public enum LANGUAGE {
        /**
         * <string name="view_language_chinese">中文</string>
         * <string name="view_language_english">English</string>
         * <string name="view_language_russian">русский</string>            俄文
         * <string name="view_language_japanese">日本語</string>
         * <string name="view_language_korean">한국어</string>             韩文
         */
        CHINA(R.string.view_language_chinese, "c", Locale.SIMPLIFIED_CHINESE, 0),
        ENGLISH(R.string.view_language_english, "e", Locale.US, 1),
        RUSSIAN(R.string.view_language_russian, "r", new Locale("ru", "RU"), 2),
        JAPANESE(R.string.view_language_japanese, "j", Locale.JAPANESE, 3),
        KOREAN(R.string.view_language_korean, "k", Locale.KOREAN, 4),
        ;
        int showRes;
        String saveChar;
        Locale outL;
        int position;

        LANGUAGE(int strRes, String c, Locale lan, int i) {
            this.showRes = strRes;
            this.saveChar = c;
            this.outL = lan;
            this.position = i;
        }
    }

//
//    public List<String> getListBySave(String old) {
//        if (old == null) return new ArrayList<>();
//        List<String> language = new ArrayList<>();
//        if ("e".equals(old) || old.startsWith("en")) {
//            language.add(lEnglish);
//            language.add(lChinese);
//            language.add(lRussian);
//            language.add(lJapanese);
//            language.add(lKorean);
//        } else if ("r".equals(old) || old.startsWith("ru")) {
//            language.add(lRussian);
//            language.add(lChinese);
//            language.add(lEnglish);
//            language.add(lJapanese);
//            language.add(lKorean);
//        } else if ("j".equals(old) || old.startsWith("ja")) {
//            language.add(lJapanese);
//            language.add(lChinese);
//            language.add(lEnglish);
//            language.add(lRussian);
//            language.add(lKorean);
//        } else if ("k".equals(old) || old.startsWith("ko")) {
//            language.add(lKorean);
//            language.add(lJapanese);
//            language.add(lChinese);
//            language.add(lEnglish);
//            language.add(lRussian);
//        } else if ("c".equals(old) || old.startsWith("zh")) {
//            language.add(lChinese);
//            language.add(lEnglish);
//            language.add(lRussian);
//            language.add(lJapanese);
//            language.add(lKorean);
//        } else {
//            language.add(lEnglish);
//            language.add(lChinese);
//            language.add(lRussian);
//            language.add(lJapanese);
//            language.add(lKorean);
//        }
//        return language;
//    }

    public void saveSelect(LANGUAGE newLocale) {
        saveSelect(newLocale.outL);
    }

    public void saveSelect(Locale newLocale) {
        String old;
        if (newLocale == Locale.SIMPLIFIED_CHINESE) {
            old = LANGUAGE.CHINA.saveChar;
        } else if (newLocale == Locale.US) {
            old = LANGUAGE.ENGLISH.saveChar;
        } else if (newLocale == new Locale("ru", "RU")) {
            old = LANGUAGE.RUSSIAN.saveChar;
        } else if (newLocale == Locale.JAPANESE) {
            old = LANGUAGE.JAPANESE.saveChar;
        } else if (newLocale == Locale.KOREAN) {
            old = LANGUAGE.KOREAN.saveChar;
        } else {
            old = LANGUAGE.CHINA.saveChar;
        }
        LOG.e("LanguageUtil", "language.save--> " + old);
        SPUtil.saveString("language", old);
        Resources resources = activity.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        Configuration config = resources.getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            config.setLocale(newLocale);
        } else {
            config.locale = newLocale;
        }
        resources.updateConfiguration(config, dm);
        activity.reloadView();
    }


    public Locale getLocale() {
        String save = SPUtil.getString("language");
        Locale newLocale;
        if (LANGUAGE.CHINA.saveChar.equals(save)) {
            newLocale = LANGUAGE.CHINA.outL;
        } else if (LANGUAGE.ENGLISH.saveChar.equals(save)) {
            newLocale = LANGUAGE.ENGLISH.outL;
        } else if (LANGUAGE.RUSSIAN.saveChar.equals(save)) {
            newLocale = LANGUAGE.RUSSIAN.outL;
        } else if (LANGUAGE.JAPANESE.saveChar.equals(save)) {
            newLocale = LANGUAGE.JAPANESE.outL;
        } else if (LANGUAGE.KOREAN.saveChar.equals(save)) {
            newLocale = LANGUAGE.KOREAN.outL;
        } else {
            newLocale = Locale.getDefault();
        }
        return newLocale;
    }


    public static Locale getLocale(int opt) {
        if (opt == LANGUAGE.CHINA.position) {
            return LANGUAGE.CHINA.outL;
        } else if (opt == LANGUAGE.ENGLISH.position) {
            return LANGUAGE.ENGLISH.outL;
        } else if (opt == LANGUAGE.RUSSIAN.position) {
            return LANGUAGE.RUSSIAN.outL;
        } else if (opt == LANGUAGE.JAPANESE.position) {
            return LANGUAGE.JAPANESE.outL;
        } else if (opt == LANGUAGE.KOREAN.position) {
            return LANGUAGE.KOREAN.outL;
        }
        return Locale.getDefault();
    }

    public int getOption() {//0-中文 1-英文 2-俄文 3-日文 4-韩文
        String save = SPUtil.getString("language");
        if (save.equals(LANGUAGE.CHINA.saveChar)) {
            return LANGUAGE.CHINA.position;
        } else if (save.equals(LANGUAGE.ENGLISH.saveChar)) {
            return LANGUAGE.ENGLISH.position;
        } else if (save.equals(LANGUAGE.RUSSIAN.saveChar)) {
            return LANGUAGE.RUSSIAN.position;
        } else if (save.equals(LANGUAGE.JAPANESE.saveChar)) {
            return LANGUAGE.JAPANESE.position;
        } else if (save.equals(LANGUAGE.KOREAN.saveChar)) {
            return LANGUAGE.KOREAN.position;
        } else {
            return 0;
        }
    }

}
